<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * MY_html_helper
 *
 * @author Simon Emms <simon@simonemms.com>
 */




/**
 * Output Databases
 * 
 * Outputs the database list
 * 
 * @param string $arrDatabases
 * @param array $arrSelected
 * @return string
 */
function output_databases($arrDatabases, $arrSelected = array()) {
    $objCI = &get_instance();
    
    if(!is_array($arrSelected) || !array_key_exists('db', $arrSelected)) {
        $arrSelected = array('db' => null);
    }
    
    $root = $objCI->database->connection_name();
    
    $html = '<ul id="database_list_wrapper">
        <li id="database_root">
            <div class="server_icon"></div>
            <a href="'.site_url().'" id="dbnode_root">'.$root.'</a>';
    
    if(count($arrDatabases) > 0) {
        $html .= '<ul>';
        foreach($arrDatabases as $_database) {
            $html .= '<li class="database_node_wrapper"><a href="'.$_database['url'].'" class="openclose_icon';
            if($arrSelected['db'] == $_database['plain']) { $html .= ' open'; }
            $html .= '"></a>';
            $html .= '<a href="'.$_database['url'].'" class="select_db">'.$_database['plain'].'</a>';
            if(count($_database['tables']) > 0) {
                $html .= '<ul class="database_table_wrapper';
                if($arrSelected['db'] != $_database['plain']) { $html .= ' hidden'; }
                $html .= '">';
                
                foreach($_database['tables'] as $_table) {
                    $html .= '<li>
                        <div class="table_icon"></div>
                        <a href="'.$_table['url'].'" class="select_tbl">'.$_table['plain'].'</a>
                    </li>';
                }
                
                $html .= '</ul>';
            }
            $html .= '</li>';
        }
        $html .= '</ul>';
    }
    
    $html .= '</li></ul>';
    
    return $html;
}




/**
 * Output Data Table
 * 
 * Outputs the HTML for a database table
 * 
 * @param array $arrTable
 * @return string
 */
function output_data_table($arrTable, $break_count = false) {
    /* Default return value */
    $return = '';
    
    /* Ensure it's an array */
    if(!is_array($arrTable)) { $arrTable = array(); }
    
    if(count($arrTable) > 0) {
        /* Get the CI instance */
        $objCI = &get_instance();
        
        /* Load form helper */
        $objCI->load->helper('form');

        /* Load the table class */
        $objCI->load->library('table');

        /* Replace true/false/null with string */
        foreach($arrTable as $tKey => $tValue) {
            if(is_array($tValue) && count($tValue) > 0) {
                $x = 1;
                foreach($tValue as $sKey => $sValue) {
                    if(is_array($sValue)) {
                        /* Enum - change to <select> */
                        $sValue = form_dropdown('name', $sValue['options'], $sValue['default']);
                    } elseif(is_null($sValue)) {
                        $sValue = '(NULL)';
                    } elseif($sValue === true) {
                        $sValue = '(TRUE)';
                    } elseif($sValue === false) {
                        $sValue = '(FALSE)';
                    } else {
                        $sValue = htmlentities($sValue);
                        
                        if(is_numeric($break_count) && strlen($sValue) > $break_count) {
                            $sValue = substr($sValue, 0, $break_count);
                            $sValue .= '&#8230;';
                        }
                    }
                    
                    $arrTable[$tKey][$sKey] = $sValue;
                }
            }
        }

        /* Get the table HTML */
        $return = $objCI->table->build($arrTable, true);
    }
    
    return $return;
}





/**
 * Output Query Results
 * 
 * Outputs the page query
 * 
 * @param array $arrQuery
 * @param number $break_count
 * @return string
 */
function output_query_results($arrQuery = null, $break_count = false) {
    $output = '';
    if(is_array($arrQuery) && array_key_exists('error', $arrQuery)) {
        if($arrQuery['error'] === false) {
            /* Valid query - is it empty */
            if(array_key_exists('empty', $arrQuery) && $arrQuery['empty']) {
                /* Yes - show empty string */
                $objCI = &get_instance();
                
                $output .= '<div class="query_results_error">';
                $output .= $objCI->lang->line('db_empty_result');
                $output .= '</div>';
            } else {
                /* No - show query */
                $output = output_data_table($arrQuery['data'], $break_count);
            }
        } else {
            /* Invalid query - show error */
            $output .= '<div class="query_results_error">';
            $output .= $arrQuery['error'];
            $output .= '</div>';
        }
    }
    return $output;
}





/**
 * Output JS
 * 
 * Outputs any JS managed by CI
 * 
 * @return string
 */
function output_js_lang() {
    /* Get CI */
    $objCI = &get_instance();
    
    /* Load stuff */
    $objCI->load->model('database');
    $objCI->load->model('sql');
    
    $arrLang = $objCI->lang->language;
    
    $output = '<script type="text/javascript">';
    $output .= 'var _objLang = {';
    if(is_array($arrLang) && count($arrLang) > 0) {
        foreach($arrLang as $key => $lang) {
            $lang = preg_replace('/(\')/', "\'", $lang);
            
            $output .= "{$key}: '{$lang}',\n";
        }
    }
    
    $output = substr($output, 0, -2);
    $output .= "\n};";
    
    if($objCI->database->verify_saved_login()) {
        $arrKeywords = $objCI->sql->get_keywords($objCI->database->get_type());

        $function = 'arrKeywords';

        $output .= 'var '.$function.' = new Array(';
        if(count($arrKeywords) > 0) {
            foreach($arrKeywords as $keyword) {
                $output .= "'".$keyword."',";
            }
            $output = substr($output, 0, -1);
        }
        $output .= ');';
    }
    
    $output .= "var _index_page = '";
    $output .= $objCI->config->item('index_page');
    $output .= "';";
    
    $output .= "var _site_url = '";
    $output .= site_url(null, false, false);
    $output .= "';";
    
    $output .= '</script>';
    
    return $output;
}





/**
 * Output UI
 * 
 * Outputs the user interface
 * 
 * @return string
 */
function output_ui() {
    $objCI = &get_instance();
    
    $objCI->load->library('user_interface');
    
    return $objCI->user_interface->output();
}







/**
 * Remove Domain
 *
 * Removes the domain from URL string
 *
 * @param string $url
 * @param bool $remove_flags
 * @return string
 */
function remove_domain($url, $remove_flags = true) {
    $objCI = &get_instance();
    $domain = preg_quote($objCI->config->item('base_url'), '/');

    /* Remove the domain */
    $url = preg_replace('(^'.$domain.')', '', $url);

    if($remove_flags) {
        
        $arrUrl = explode('/', $url);

        if(count($arrUrl) > 0) {
            foreach($arrUrl as $key => $value) {
                if($key < 2) {
                    /* Check for language flag */
                    if($value == $objCI->config->item('language')) {
                        unset($arrUrl[$key]);
                    }

                    /* Check for mobile flag */
                    if($value == $objCI->config->item('mobile_flag')) {
                        unset($arrUrl[$key]);
                    }
                }
            }
        }

        /* Implode it back */
        $url = implode('/', $arrUrl);
    }

    return $url;
}





/**
 * Remove Slash
 *
 * Removes excess slashes from
 * a string
 *
 * @param string $string
 * @return string
 */
function remove_slash($string) {
    /* Check for an http(s)/ftp at the start */
    if(preg_match('/^(\w*)\:\/\//', $string, $match)) {
        /* There's a legitimate double slash - keep it */
        $return = isset($match[0]) ? $match[0] : '';
        $arrString = preg_split('/^(\w*)\:\/\//', $string);
        if(count($arrString) > 0) {
            foreach($arrString as $string) {
                if(!empty($string)) {
                    $return .= remove_slash($string);
                }
            }
        }
        return $return;
    } else {
        /* No legitimate double slashes - get rid of them all */
        return preg_replace('/(\w)?(\/\/+)/', '\\1/', $string);
    }
}









/**
 * Image
 *
 * Generates an <img /> element
 *
 * @access	public
 * @param	mixed
 * @return	string
 */
function img($src = '', $index_page = true)
{
        if ( ! is_array($src) )
        {
                $src = array('src' => $src);
        }

        // If there is no alt attribute defined, set it to an empty string
        if ( ! isset($src['alt']))
        {
                $src['alt'] = '';
        }

        $img = '<img';

        foreach ($src as $k=>$v)
        {

                if ($k == 'src' AND strpos($v, '://') === FALSE)
                {
                        $CI =& get_instance();

                        if ($index_page === TRUE)
                        {
                                $img .= ' src="'.remove_slash(interface_url($v)).'"';
                        }
                        else
                        {
                                $img .= ' src="'.remove_slash($CI->config->slash_item('base_url').$v).'"';
                        }
                }
                else
                {
                        $img .= " $k=\"$v\"";
                }
        }

        $img .= '/>';

        return $img;
}

?>